package com.sstir.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sstir.doctor.common.BusinessResponseEnum;
import com.sstir.doctor.common.DifficultyEnum;
import com.sstir.doctor.entity.UserInfoEntity;
import com.sstir.doctor.mapper.UserInfoMapper;
import com.sstir.doctor.mapper.UserInfoVOConvert;
import com.sstir.doctor.pojo.bo.RankingListBO;
import com.sstir.doctor.pojo.vo.RankingListVO;
import com.sstir.doctor.pojo.vo.UserScoreVO;
import com.sstir.doctor.result.RespResult;
import com.sstir.doctor.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hanjunjun
 * @since 2021-01-15
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoEntity> implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public RespResult registerUsername(String username) {
        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        List<UserInfoEntity> userInfoEntityList = list(queryWrapper);
        if (!CollectionUtils.isEmpty(userInfoEntityList)){
            return RespResult.badRequest(BusinessResponseEnum.USER_ALREADY_REG);
        }
        //入库
        UserInfoEntity entity = new UserInfoEntity();
        entity.setUserName(username);
        if (userInfoMapper.insertUserSelectiveCallId(entity) <= 0){
            return RespResult.badRequest(BusinessResponseEnum.BUSINESS_EXCEPTION);
        }
        log.info("生成的id：{}", entity.getId());
        return RespResult.ok(entity.getId());
    }

    @Override
    public RespResult getRankingList(RankingListBO rankingListBO) {
        //分页查询
        Page<UserInfoEntity> page = new Page<>(rankingListBO.getCurrent(), rankingListBO.getSize());
        QueryWrapper<UserInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_diff", rankingListBO.getUserDiff()).orderByDesc("user_score");
        IPage<UserInfoEntity> resultPage = page(page, queryWrapper);
        List<UserInfoEntity> entityList = resultPage.getRecords();
        if (CollectionUtils.isEmpty(entityList)){
            return RespResult.ok(entityList);
        }

        //封装
        List<RankingListVO> resultList = new ArrayList<>();
        for (UserInfoEntity entity : entityList){
            RankingListVO vo = new RankingListVO();
            BeanUtils.copyProperties(entity, vo);
            //难度赋值
            vo.setUserDiff(DifficultyEnum.getLabelByCode(Integer.parseInt(vo.getUserDiff())));
            //时间
            vo.setInsertTime(entity.getInsertTime());
            resultList.add(vo);
        }
        return RespResult.ok(resultList);
    }

    @Override
    public RespResult submitCore(UserScoreVO userScoreVO) {
        Assert.notNull(userScoreVO, BusinessResponseEnum.PARAM_IS_NULL.getMessage());

//        List<UserInfoEntity> list = list(new QueryWrapper<UserInfoEntity>().eq("user_name", userScoreVO.getUsername()));
//        if (CollectionUtils.isEmpty(list)){
            UserInfoEntity entity = UserInfoVOConvert.INSTANCE.convertUserScoreVO2Entity(userScoreVO);
            if (!save(entity)){
                return RespResult.badRequest(BusinessResponseEnum.BUSINESS_EXCEPTION);
            }
            return RespResult.ok();
//        }

        /*//更新
        UserInfoEntity userInfoEntity = list.get(0);
        if (userScoreVO.getScore() > userInfoEntity.getUserScore()){
            userInfoEntity.setUserScore(userScoreVO.getScore());
            userInfoEntity.setUserDiff(userScoreVO.getUserDiff());
            if (!updateById(userInfoEntity)){
                return RespResult.badRequest(BusinessResponseEnum.BUSINESS_EXCEPTION);
            }
        }
        return RespResult.ok();*/
    }
}
